package mapkit.layers
{

	public class TileGrid
	{
		private var _level:int=0;

		public function get level():int
		{
			return _level;
		}

		private var _mincol:int=0;

		public function get minCol():int
		{
			return _mincol;
		}

		private var _minrow:int=0;

		public function get minRow():int
		{
			return _minrow;
		}

		private var _maxcol:int=0;

		public function get maxCol():int
		{
			return _maxcol;
		}

		private var _maxrow:int=0;

		public function get maxRow():int
		{
			return _maxrow;
		}

		private var _offsetx:Number=0;

		public function get offsetX():int
		{
			return _offsetx;
		}

		private var _offsety:Number=0;

		public function get offsetY():int
		{
			return _offsety;
		}

		public function TileGrid(minx_:int, miny_:int, maxx_:int, maxy_:int,
			level_:int, offsetx_:Number=0, offsety_:Number=0)
		{
			_mincol=minx_;
			_minrow=miny_;
			_maxcol=maxx_;
			_maxrow=maxy_;
			_level=level_;
			_offsetx=offsetx_;
			_offsety=offsety_;
		}

		public function getTileInfos(value:TileGrid=null):Array
		{
			var hash:Object=new Object();
			var cursor:TileInfo=null;
			var result:Array=new Array();

			for (var ocol:int=minCol; ocol <= maxCol; ocol++)
			{
				for (var orow:int=minRow; orow <= maxRow; orow++)
				{
					cursor=new TileInfo(ocol, orow, level, TileInfo.ADD);
					hash[cursor.toString()]=cursor;
				}
			}

			if (value == null)
			{
				for each (var tile1:TileInfo in hash)
				{
					result.push(tile1);
				}
				return result;
			}

			for (var scol:int=value.minCol; scol <= value.maxCol; scol++)
			{
				for (var srow:int=value.minRow; srow <= value.maxRow; srow++)
				{
					cursor=new TileInfo(scol, srow, value.level, TileInfo.REMOVE);
					if (hash[cursor.toString()])
					{
						cursor.operator=TileInfo.IGNORE;
					}
					else
					{
						hash[cursor.toString()]=cursor;
					}
				}
			}

			for each (var tile2:TileInfo in hash)
			{
				result.push(tile2);
			}
			return result;
		}

		public function clone():TileGrid
		{
			return new TileGrid(minCol, minRow, maxCol, maxRow, level, offsetX,
				offsetY);
		}

	}
}